#pragma once
#include <iostream>
#include <fstream>
#include <set>
#include <string>

static const char* safeFilePath = "./safe.txt";

class CommandExcute
{
    void Load()
    {
        std::ifstream ifs(safeFilePath, std::ios::in);
        if(!ifs.is_open())
        {
            std::cout << "load fail\n";
            exit(1);
        }
        std::string str;
        while(ifs >> str)
        {
            _safe_command.insert(str);
        }
    }
public:
    CommandExcute()
    {
        Load();
    }
    std::string Excute(const std::string& cmd)
    {
        if(_safe_command.count(cmd))
        {
            FILE* pf = popen(cmd.c_str(), "r");
            if(pf == nullptr)
            {
                return "None";
            }
            std::string ret;
            int t = fgetc(pf);
            while(t != EOF)
            {
                ret += t;
                t = fgetc(pf);
            }
            pclose(pf);
            return ret;
        }
        else
        {
            return "error cmd";
        }
    }
    ~CommandExcute()
    {}
private:
    std::set<std::string> _safe_command;
};